package zut.wework.init;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import zut.wework.entity.BzksBj;
import zut.wework.entity.BzksYx;
import zut.wework.entity.BzksZy;
import zut.wework.entity.TreeSelect;
import zut.wework.mapper.BzksBjMapper;
import zut.wework.mapper.BzksYxMapper;
import zut.wework.mapper.BzksZyMapper;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: Mr.Liu
 * @create: 2022-04-21 13:06
 */
@Component
public class InitializationDept {


    @Autowired
    private BzksZyMapper bzksZyMapper;

    @Autowired
    private BzksYxMapper bzksYxMapper;

    @Autowired
    private BzksBjMapper bzksBjMapper;

    public Map<String,BzksZy> zyMap = new HashMap<>();
    public Map<String,BzksYx> yxMap = new HashMap<>();

    public Map<Integer,Set<BzksYx>> allMap= new HashMap<>();

    public int year = LocalDateTime.now().getYear();


    public TreeSelect treeSelect;

    private static final ReentrantLock initLock = new ReentrantLock();

//    @PostConstruct
    private void init(){
        List<BzksZy> bzksZyList = bzksZyMapper.selectAll();//专业
        List<BzksYx> bzksYxList = bzksYxMapper.selectAll();//院系

        //把院系代码作为键
        bzksYxList.forEach(i->yxMap.put(i.getDm(),i));
        bzksZyList.forEach(i->zyMap.put(i.getDwdm(),i));

        int month = LocalDateTime.now().getMonth().getValue();

        if (month<7){
            year--;
        }

        bzksZyList.forEach(zy->{
            int xz = zy.getXz();//学制几年
            for (int i = 0,y = year; i < xz; i++,y--) {
                if (!allMap.containsKey(y)){
                    allMap.put(y,new HashSet<>());
                }
                if (yxMap.containsKey(zy.getDwdm())) {
                    BzksYx bzksYx = yxMap.get(zy.getDwdm());
                    allMap.get(y).add(bzksYx);
                }
            }
        });
//        System.out.println("+++"+allMap.toString());
    }


//    @PostConstruct
    public void getTreeList(){
        initLock.lock();
        try {
            TreeSelect ts = new TreeSelect();
            ts.setLabel("花瓣舞者");
            ArrayList<TreeSelect> list = new ArrayList<>();
            ts.setChildren(list);
            TreeSelect bks = new TreeSelect();
            bks.setLabel("本专科生");
            list.add(bks);
            ArrayList<TreeSelect> y = new ArrayList<>();
            bks.setChildren(y);

            Iterator<Integer> iterator = allMap.keySet().iterator();
            while (iterator.hasNext()){
                Integer ys = iterator.next();
                TreeSelect t = new TreeSelect();
                t.setLabel(ys.toString());
                t.setId(ys.toString());

                //院系
                ArrayList<TreeSelect> yx = new ArrayList<>();
                for (BzksYx bzksYx : allMap.get(ys)) {
                    TreeSelect yxt = new TreeSelect();
                    yxt.setLabel(bzksYx.getMc());
                    yxt.setId(bzksYx.getDm());
                    //专业
                    ArrayList<TreeSelect> zy = new ArrayList<>();
                    List<BzksZy> bzksZyList = bzksZyMapper.selectZyById(bzksYx.getDm(), year-ys);
                    for (int i = 0; i < bzksZyList.size(); i++) {
                        TreeSelect zys = new TreeSelect();
                        zys.setLabel(bzksZyList.get(i).getMc());
                        zys.setId(bzksZyList.get(i).getDm());
                        //班级
                        ArrayList<TreeSelect> bj = new ArrayList<>();
                        List<BzksBj> bzksBjs = bzksBjMapper.selectList(new QueryWrapper<BzksBj>().eq("zydm", bzksZyList.get(i).getDm()).eq("nj", ys));
                        for (int j = 0; j < bzksBjs.size(); j++) {
                            TreeSelect bjs = new TreeSelect();
                            bjs.setLabel(bzksBjs.get(j).getBjmc());
                            bjs.setId(bzksBjs.get(j).getBh());
                            bj.add(bjs);
                        }


                        zys.setChildren(bj);
                        zy.add(zys);
                    }


                    yxt.setChildren(zy);
                    yx.add(yxt);
                }


                t.setChildren(yx);
                y.add(t);
            }

            treeSelect = ts;
        } finally {
            initLock.unlock();
        }
    }

}
